Ext.onReady(function() {

    Ext.QuickTips.init();
    // 元数据的store
    var store2 = new Ext.data.Store({
        storeId: 'store2',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=property'
        }),
        reader: new Ext.data.JsonReader({
            fields: [
                {
                    name: 'name'
                },
                {
                    name: 'value'
                }
            ]
        })
    });

    var driverList_store = new Ext.data.Store({
        storeId: 'store4',
        proxy: new Ext.data.HttpProxy({
            url: 'Get_Data_List.action?method=driver'
        }),
        reader: new Ext.data.JsonReader({
            fields: [
                {
                    name: 'name'
                }, {
                    name: 'value'
                }
            ]
        })
    });

    driverList_store.load();




    var toolbar = new Ext.Toolbar({
        id: 'toolbar',
        stateful: true,
        items: ['-', {
                id: 'add-p',
                text: '新增',
                handler: function() {
                    driver_add_win.show();
                }
            }, '-', {
                id: 'edit-btn',
                text: '编辑',
                handler: function() {
                    edit();
                }
            }, '-', {
                id: 'del-btn',
                text: '删除',
                handler: function() {
                    Ext.MessageBox.show({
                        title: '提示框',
                        msg: ' 确定删除该驾驶员吗？注意，数据删除后将无法恢复！ ',
                        buttons: {
                            'yes': '确定',
                            'no': '取消'
                        },
                        fn: showResult,
                        icon: Ext.MessageBox.QUESTION
                    });

                    function showResult(btn) {
                        if (btn == 'yes') {
                            del();
                        }
                    }

                }
            }, '-', {
                text: '导出Excel',
                handler: function() {
                    window.open("ExportXls_Driver.action");
                }
            }, '-']
    });

    var form = new Ext.FormPanel(
            {
                labelAlign: 'form',
                frame: true,
                bodyStyle: 'padding:5px 5px 0',
                width: '100%',
                items: [
                    {
                        layout: 'column',
                        items: [
                            {
                                columnWidth: .3,
                                layout: 'form',
                                items: [
                                    new Ext.form.ComboBox({
                                        fieldLabel: '驾驶员',
                                        name: 'name_form',
                                        id: 'name_form',
                                        displayField: 'name',
                                        valueField: 'name',
                                        emptyText: '驾驶员',
                                        anchor: '95%',
                                        typeAhead: true,
                                        triggerAction: 'all',
                                        queryParam: 'name',
                                        minChars: 2,
                                        store: driverList_store
                                    }),
                                    {
                                        xtype: 'combo',
                                        fieldLabel: '性别',
                                        name: 'sex_form',
                                        id: 'sex_form',
                                        emptyText: '性别',
                                        triggerAction: 'all',
                                        anchor: '95%',
                                        store: [
                                            ['1', '男'],
                                            ['2', '女']
                                        ]
                                    },
                                    {
                                        xtype: 'textfield',
                                        fieldLabel: '身份证号码',
                                        emptyText: '身份证号码',
                                        name: 'id_card_form',
                                        id: 'id_card_form',
                                        anchor: '95%'
                                    }
                                ]
                            },
                            {
                                columnWidth: .3,
                                layout: 'form',
                                items: [
                                    {
                                        xtype: 'textfield',
                                        fieldLabel: '准驾车型',
                                        emptyText: '准驾车型',
                                        name: 'car_model_form',
                                        id: 'car_model_form',
                                        anchor: '95%'
                                    },
                                    {
                                        xtype: 'textfield',
                                        fieldLabel: '手机号码',
                                        emptyText: '手机号码',
                                        name: 'phone_num_form',
                                        id: 'phone_num_form',
                                        anchor: '95%'
                                    },
                                    new Ext.form.ComboBox({
                                        fieldLabel: '是否兼职',
                                        name: 'sideline_form',
                                        id: 'sideline_form',
                                        emptyText: '是否兼职',
                                        displayField: 'name',
                                        valueField: 'value',
                                        triggerAction: 'all',
                                        anchor: '95%',
                                        store: [
                                            ['yes', '是'],
                                            ['no', '否']
                                        ]
                                    })
                                ]
                            },
                            {
                                columnWidth: .3,
                                layout: 'form',
                                items: [
                                    {
                                        xtype: 'datefield',
                                        altFormats: 'Ymd',
                                        format: 'Ymd',
                                        dateFormat: 'Ymd',
                                        fieldLabel: '出生年月',
                                        emptyText: '出生年月',
                                        name: 'born_form_form',
                                        id: 'born_form_form',
                                        anchor: '95%'
                                    },
                                    {
                                        xtype: 'datefield',
                                        altFormats: 'Ymd',
                                        format: 'Ymd',
                                        dateFormat: 'Ymd',
                                        fieldLabel: '到',
                                        emptyText: '到',
                                        name: 'born_end_form',
                                        id: 'born_end_form',
                                        anchor: '95%'
                                    }
                                ]
                            },
                            {
                                columnWidth: .1,
                                layout: 'form',
                                items: [
                                    {
                                        name: 'iPage',
                                        id: 'iPage',
                                        xtype: 'hidden'
                                    },
                                    {
                                        xtype: "button",
                                        id: 'query_tbar',
                                        text: "查  询",
                                        width: 60,
                                        handler: function() {
                                            store.setBaseParam("name", Ext.ComponentMgr.get("name_form").getValue());
                                            store.setBaseParam("sex",
                                                    Ext.ComponentMgr
                                                    .get(
                                                            "sex_form")
                                                    .getValue());
                                            store
                                                    .setBaseParam(
                                                            "id_code",
                                                            Ext.ComponentMgr
                                                            .get(
                                                                    "id_card_form")
                                                            .getValue());
                                            store
                                                    .setBaseParam(
                                                            "car_model",
                                                            Ext.ComponentMgr
                                                            .get(
                                                                    "car_model_form")
                                                            .getValue());
                                            store
                                                    .setBaseParam(
                                                            "phone_num",
                                                            Ext.ComponentMgr
                                                            .get(
                                                                    "phone_num_form")
                                                            .getValue());
                                            store
                                                    .setBaseParam(
                                                            "sideline",
                                                            Ext.ComponentMgr
                                                            .get(
                                                                    "sideline_form")
                                                            .getValue());
                                            store
                                                    .setBaseParam(
                                                            "born_form",
                                                            Ext.util.Format
                                                            .date(
                                                                    Ext.ComponentMgr
                                                                    .get(
                                                                            "born_form_form")
                                                                    .getValue(),
                                                                    'Ymd'));
                                            store
                                                    .setBaseParam(
                                                            "born_end",
                                                            Ext.util.Format
                                                            .date(
                                                                    Ext.ComponentMgr
                                                                    .get(
                                                                            "born_end_form")
                                                                    .getValue(),
                                                                    'Ymd'));
                                            store.load();
                                        }
                                    }
                                ]
                            }
                        ]
                    }
                ]
            });

    jReader1 = new Ext.data.JsonReader({
        root: 'data',
        totalProperty: 'totalCount',
        fields: [
            {
                name: 'id'
            },
            {
                name: 'name'
            },
            {
                name: 'sex'
            },
            {
                name: 'property'
            },
            {
                name: 'head_image'
            },
            {
                name: 'born'
            },
            {
                name: 'id_code'
            },
            {
                name: 'phone_num'
            },
            {
                name: 'car_model'
            },
            {
                name: 'sideline'
            },
            {
                name: 'experience'
            },
            {
                name: 'glory'
            },
            {
                name: 'ab'
            }
        ]
    });

    var store = new Ext.data.Store({
        storeId: 'store1',
        proxy: new Ext.data.HttpProxy({
            url: 'DriverJson.action'
        }),
        reader: jReader1,
        baseParams: {
        }
    });

    var sm = new Ext.grid.CheckboxSelectionModel({
    });

    var cm = new Ext.grid.ColumnModel([sm, {
            header: '编号',
            sortable: true,
            hidden: true,
            dataIndex: 'id'
        }, {
            header: '姓名',
            sortable: true,
            width: 69,
            dataIndex: 'name'
        }, {
            header: '性别',
            sortable: true,
            width: 50,
            dataIndex: 'sex',
            renderer: function(v) {
                if (v == 1) {
                    return "男";
                } else if (v == 2) {
                    return "女";
                }
            }
        }, {
            header: '出生年月',
            sortable: true,
            width: 72,
            dataIndex: 'born'
        }, {
            header: '身份证号码',
            sortable: true,
            width: 141,
            dataIndex: 'id_code'
        }, {
            header: '电话号码',
            sortable: true,
            dataIndex: 'phone_num'
        }, {
            header: '准驾车型',
            sortable: true,
            width: 68,
            dataIndex: 'car_model'
        }, {
            header: '是否兼职',
            sortable: true,
            width: 75,
            dataIndex: 'sideline',
            renderer: function(v) {
                if (v == 'yes') {
                    return "是";
                } else if (v == 'no') {
                    return "否";
                }
            }
        }, {
            header: '工作经历',
            width: 180,
            //  hidden:true,
            dataIndex: 'experience'
        }, {
            header: '个人荣誉',
            width: 180,
            // hidden:true,
            dataIndex: 'glory'
        }, {
            header: '缩写',
            width: 72,
            dataIndex: 'ab'
        }, {
            header: '头像',
            sortable: true,
            hidden: true,
            dataIndex: 'head_image'
        }]);

    var grid = new Ext.grid.GridPanel({
        id: 'grid_driver',
        store: store,
        anchor: '100% -100',
        viewConfig: {
            forceFit: false
        },
        sm: sm,
        cm: cm,
        frame: true,
        columnLines: true,
        autoWidth: true,
        loadMask: true, // 数据加载时的遮罩功能
        stateful: true,
        stateId: 'grid'
//        pageSize: 20,
//        bbar: new Ext.PagingToolbar({
//            store: store,
//            emptyMsg: '无数据',
//            displayInfo: true
//        })
    });
    store.load();

    grid.addListener('rowdblclick', rowdblclickFn);
    function rowdblclickFn(grid, rowindex, e) {
        var images = "<br><div><img  src='"
                + basePath + store.data.items[rowindex].data.head_image
                + "'/></div>";
        head_image_win.show();
        Ext.getCmp("head_image_win").update(images);

    }

    var viewport = new Ext.Viewport({
        layout: 'border',
        items: [
            {
                region: 'center',
                tbar: toolbar,
                layout: 'border',
                items: [
                    {
                        region: 'center',
                        // layout : 'accordion',
                        layout: "anchor",
                        frame: true,
                        deferredRender: false,
                        activeTab: 0,
                        items: [form, grid]
                    }
                ]
            }
        ]
    });

    /**
     * 删除驾驶员数据
     */
    function del() {
        var del_data = Ext.ComponentMgr.get("grid_driver")
                .getSelectionModel().getSelections();
        if (del_data.length == 0) {
            Ext.Msg.alert('提示', '请选择需要删除的驾驶员数据！');
            return;
        } else {
            var id_array = [];
            for (var i = 0; i < del_data.length; i++) {
                id_array[i] = del_data[i].data.id;
            }
            sendRequest(id_array);
        }
    }

    /**
     * 修改驾驶员数据
     */
    function edit() {
        var edit_data = Ext.ComponentMgr.get("grid_driver")
                .getSelectionModel().getSelections();
        if (edit_data.length != 1) {
            Ext.Msg.alert('提示', '对不起，同时只能修改一条数据');
            return;
        } else {
            driver_add_win.show();
            driver.getForm().loadRecord(edit_data[0]);
            //Ext.getCmp("driver").getForm().name=edit_data.data.name;
            //Ext.ComponentMgr.get("driver_sex").setValue(edit_data.data.sex);

        }
    }

    function sendRequest(data) {
        Ext.Ajax.request({
            url: 'DeleteAction.action',
            params: {
                id: data,
                type: 'driver'
            },
            success: function(response, options) {
                var responseArray = Ext.util.JSON
                        .decode(response.responseText);
                if (responseArray.success == true) {
                    Ext.Msg.alert('提示', '删除成功！');
                    store.reload();
                } else {
                    Ext.Msg.alert('提示', '删除失败！');
                }
            },
            failure: function(response, options) {
                Ext.Msg.alert('提示', '删除失败！');
            }
        });
    }


    var store_pro = new Ext.data.Store({
        storeId: 'store1',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=property'
        }),
        reader: new Ext.data.JsonReader({
            fields: [
                {
                    name: 'name'
                },
                {
                    name: 'value'
                }
            ]
        })
    });

    store_pro.load();

    var form = new Ext.form.FormPanel({
        baseCls: 'x-plain',
        labelWidth: 70,
        labelHeight: 150,
        fileUpload: true,
        defaultType: 'textfield',
        items: [
            {
                xtype: 'textfield',
                fieldLabel: '选择文件',
                name: 'userfile',
                id: 'userfile',
                inputType: 'file',
                anchor: '95%' // anchor width by percentage
            }
        ]

    });
    var win_upload = new Ext.Window({
        title: '上传文件',
        width: 450,
        height: 180,
        modal: true,
        x: 100,
        y: 50,
        layout: 'form',
        autoScroll: true,
        constrain: true,
        bodyStyle: 'padding:10px 10px 10px 10px;',
        items: form,
        buttons: [
            {
                text: '确认上传',
                handler: function() {
                    if (form.form.isValid()) {
                        if (Ext.getCmp('userfile').getValue() == '') {
                            Ext.Msg.alert('提示', '请选择要上传的文件');
                            return;
                        }
                        Ext.MessageBox.show({
                            title: '请稍后....',
                            msg: '文件正在上传中....',
                            progressText: '',
                            width: 300,
                            progress: true,
                            closable: false,
                            animEl: 'loding'
                        });
                        form.getForm().submit({
                            url: basePath + 'servlet/Upload',
                            method: 'POST',
                            success: function(form, action) {
                                Ext.Msg.alert('成功', '文件上传成功！');
                                win_upload.hide();
                                var images = "<br><div><img width='120' height='140'  src='" + basePath + action.result.url + "'/></div>";
                                Ext.getCmp("image_panel").update(images);
                                Ext.getCmp("head_image").setValue(action.result.url);
                            },
                            failure: function(form, action) {
                                Ext.Msg.alert('错误', "文件上传失败，请重新操作！");
                            }
                        })
                    }
                }
            },
            {
                text: '关闭',
                handler: function() {
                    win_upload.hide();
                }
            }
        ],
        closable: false,
        draggable: false,
        resizable: false
    });

    var driver = new Ext.FormPanel({
        id: 'driver',
        name: 'driver',
        labelWidth: 80,
        width: '100%',
        height: '100%',
        bodyStyle: 'padding: 4px',
        border: false,
        autoscroll: true,
        frame: true,
        autoScroll: true,
        items: [
            {
                xtype: "hidden",
                fieldLabel: "编号",
                name: 'id',
                anchor: "80%"
            },
            {
                xtype: "textfield",
                fieldLabel: "驾驶员姓名*",
                emptyText: '驾驶员姓名(必填)',
                disabled: false,
                allowBlank: false,
                id: 'driver_name',
                name: 'name',
                anchor: "80%"
            },
            {
                xtype: 'hidden',
                name: 'head_image',
                id: 'head_image'
            },
            {
                xtype: 'panel',
                id: 'image_panel',
                fieldLabel: '头像',
                items: [new Ext.Button({
                        text: '选择图片',
                        width: 50,
                        tooltipType: 'title',
                        handler: function() {
                            win_upload.show();

                        }
                    })],
                html: ''
            },
            {
                xtype: "radiogroup",
                fieldLabel: "性别",
                emptyText: '性别',
                disabled: false,
                id: 'driver_sex',
                anchor: "80%",
                items: [
                    {
                        boxLabel: '男',
                        name: 'sex',
                        inputValue: '1',
                        checked: true
                    },
                    {
                        boxLabel: '女',
                        name: 'sex',
                        inputValue: '2'
                    }
                ]
            },
            {
                xtype: 'datefield',
                fieldLabel: '出生年月',
                name: 'born',
                id: 'born_date',
                altFormats: 'Ymd',
                format: 'Ymd',
                dateFormat: 'Ymd',
                // columnWidth: .9,
                emptyText: '出生年月',
                anchor: "80%"
            },
            {
                xtype: "textfield",
                fieldLabel: "身份证号码",
                emptyText: '身份证号码',
                disabled: false,
                id: 'id_code',
                name: 'id_code',
                anchor: "80%"
            },
            {
                xtype: "textfield",
                fieldLabel: "手机号码",
                emptyText: '手机号码',
                disabled: false,
                id: 'phone_num',
                name: 'phone_num',
                anchor: "80%"
            },
            {
                xtype: "textfield",
                fieldLabel: "准驾车型",
                emptyText: '准驾车型',
                disabled: false,
                id: 'car_model',
                name: 'car_model',
                anchor: "80%"
            },
            {
                xtype: "radiogroup",
                fieldLabel: "是否兼职",
                emptyText: '',
                disabled: false,
                id: 'driver_jianzhi',
                anchor: "80%",
                items: [
                    {
                        boxLabel: '是',
                        name: 'sideline',
                        inputValue: 'yes'
                    },
                    {
                        boxLabel: '否',
                        name: 'sideline',
                        inputValue: 'no',
                        checked: true
                    }
                ]
            },
            {
                xtype: "textfield",
                fieldLabel: "工作经历",
                emptyText: '工作经历',
                disabled: false,
                id: 'experience',
                name: 'experience',
                anchor: "80%"
            },
            {
                xtype: "textfield",
                fieldLabel: "个人荣誉",
                emptyText: '个人荣誉',
                disabled: false,
                id: 'glory',
                name: 'glory',
                anchor: "80%"
            },
            {
                xtype: "textfield",
                fieldLabel: "缩写",
                emptyText: '缩写',
                disabled: false,
                id: 'ab',
                name: 'ab',
                anchor: "80%"
            }
        ]
    });

    var driver_add_win = new Ext.Window({
        id: 'driver_add_win',
        title: "添加驾驶员",
        border: false,
        bodyBorder: false,
        // layout:'fit',
        width: 415,
        closeAction: 'hide',
        modal: true,
        tbar: ['-', {
                text: ' 提交并继续',
                handler: function() {
                    onSubmit();

                }
            }, '-', {
                text: ' 提交并返回',
                handler: function() {
                    onSubmit();
                    driver_add_win.hide();
                }
            }, '-', {
                text: ' 返回',
                handler: function() {
                    driver.getForm().reset();
                    driver_add_win.hide();
                }
            }, '-', {
                text: ' 重填',
                handler: function() {
                    driver.getForm().reset();
                }
            }, '-'],
        items: [driver]
    });

    function onSubmit() {
        // 提交表单
        if (driver.getForm().isValid()) {
            var submitValues = driver.getForm().getValues();
            for (var param in submitValues) {
                if (driver.form.findField(param)
                        && driver.form.findField(param).emptyText == submitValues[param]) {
                    driver.form.findField(param).setValue(' ');
                }
            }
            driver.getForm().submit({
                url: 'DriverAction.action',
                waitMsg: '数据保存中...',
                submitEmptyText: false,
                success: function(c, d) {
                    Ext.MessageBox.alert("操作信息", "成功信息保存！");
                    driver.getForm().reset();
                    store.reload();
                },
                failure: function(c, d) {
                    Ext.MessageBox.show({
                        title: "操作信息",
                        msg: "信息保存出错，请联系管理员！",
                        buttons: Ext.MessageBox.OK,
                        icon: "ext-mb-error"
                    });
                }
            });
        }
    }

    var head_image_win = new Ext.Window({
        title: "驾驶员照片",
        border: false,
        id: 'head_image_win',
        bodyBorder: false,
        // layout:'fit',

        closeAction: 'hide',
        modal: true
    });

});